﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Net;
using System.Text.RegularExpressions;

namespace CodePlexNewReleaseChecker
{
    public class VersionChecker
    {
        public string ProjectName { get; private set; }
        public IReleaseNamesProvider ReleaseProvider { get; private set; }

        public VersionChecker(string projectName,
            IReleaseNamesProvider provider)
        {
            this.ProjectName = projectName;
            this.ReleaseProvider = provider;
        }

        public Release GetDefaultRelease()
        {
            var releases = GetReleases();
            return releases.Where(x => x.DefaultRelease).FirstOrDefault();
        }

        public Release GetReleaseByName(string name)
        {
            var item = (from c in ReleaseProvider.GetReleaseNames(this.ProjectName)
                        where c.Name == name
                        select c);

            if (!item.Any()) return null;

            return new Release(ProjectName, item.First());
        }

        public IEnumerable<Release> GetReleases()
        {
            var items = ReleaseProvider.GetReleaseNames(this.ProjectName);

            return (from c in items select new Release(ProjectName, c)).ToList();
        }

    }
}
